package com.honghu.villagepost.activity.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.honghu.villagepost.MainActivity;
import com.honghu.villagepost.R;
import com.honghu.villagepost.activity.AddrActivity;
import com.honghu.villagepost.activity.ContactUsActivity;
import com.honghu.villagepost.activity.LoginActivity;
import com.honghu.villagepost.activity.OrderListActivity;
import com.honghu.villagepost.activity.PersonActivity;
import com.honghu.villagepost.activity.WebViewActivity;
import com.honghu.villagepost.api.ApiService;
import com.honghu.villagepost.base.BaseFragment;
import com.honghu.villagepost.callback.DialogCallback;
import com.honghu.villagepost.callback.LzyResponse;
import com.honghu.villagepost.constant.Constants;
import com.honghu.villagepost.dao.manager.DbManager;
import com.honghu.villagepost.entity.User;
import com.honghu.villagepost.util.DensityUtil;
import com.honghu.villagepost.util.GlideUtil;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.interfaces.SimpleCallback;
import com.lzy.okgo.model.Response;
import com.wuhenzhizao.sku.utils.ScreenUtils;

public class MyFragment extends BaseFragment {
    private ConstraintLayout mLayoutInfo;
    private ImageView iv_photo;
    private TextView tv_name;
    private TextView tv_phone;

    @Override
    protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        return view;
    }

    @Override
    protected void initData() {
        ((MainActivity)getActivity()).setStatusBarTintResource(R.color.transparent);
        int statusBarHeight = ScreenUtils.getStatusBarHeight(getActivity());
        mLayoutInfo = getView().findViewById(R.id.layout_info);
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mLayoutInfo.getLayoutParams();
        params.height = statusBarHeight + DensityUtil.dp2px(205);
        mLayoutInfo.setPadding(0, statusBarHeight, 0, 0);

        iv_photo = getView().findViewById(R.id.iv_photo);
        tv_name = getView().findViewById(R.id.tv_name);
        tv_phone = getView().findViewById(R.id.tv_phone);

        getView().findViewById(R.id.tv_all).setOnClickListener(v -> {
            ActivityUtils.startActivity(OrderListActivity.class);
        });

        getView().findViewById(R.id.tv_obligation).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.TYPE, 1);
                ActivityUtils.startActivity(OrderListActivity.class);
            }
        });

        getView().findViewById(R.id.tv_receiving).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.TYPE, 3);
                ActivityUtils.startActivity(OrderListActivity.class);
            }
        });

        getView().findViewById(R.id.layout_info).setOnClickListener(v -> {
            ActivityUtils.startActivity(PersonActivity.class);
        });

        getView().findViewById(R.id.tv_person).setOnClickListener(v -> {
            ActivityUtils.startActivity(PersonActivity.class);
        });

        getView().findViewById(R.id.tv_addr).setOnClickListener(v -> {
            ActivityUtils.startActivity(AddrActivity.class);
        });

        getView().findViewById(R.id.tv_contact).setOnClickListener(v -> {
            ActivityUtils.startActivity(ContactUsActivity.class);
        });

        getView().findViewById(R.id.tv_logout).setOnClickListener(v -> {
            new XPopup.Builder(getContext())
//                         .dismissOnTouchOutside(false)
//                         .autoDismiss(false)
//                        .popupAnimation(PopupAnimation.NoAnimation)
                    .asConfirm("", "是否确认退出登录？",
                    "取消", "确定",
                    new OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            SPUtils.getInstance().put(Constants.TOKEN, "");
                            LoginActivity.self(getActivity());
                            DbManager.getInstance().loginOut();
                        }
                    }, null, false)
                    .show();

        });

        getView().findViewById(R.id.tv_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(getActivity(), "服务协议", Constants.SERVICE_PROTOCOL);
            }
        });

        getView().findViewById(R.id.tv_protocol).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(getActivity(), "隐私政策", Constants.PRIVATE_PROTOCOL);
            }
        });

        getInfo();

    }

    void getInfo() {
        ApiService.user_info(this, new DialogCallback<LzyResponse<User>>() {
            @Override
            public void onSuccess(Response<LzyResponse<User>> response) {
                if (iv_photo != null){
                    GlideUtil.loadCircleImg(iv_photo, response.body().data.getAvatar());
                    tv_name.setText(response.body().data.getNickname());
                    tv_phone.setText(response.body().data.getMobile());
                }

            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            if (getActivity() != null){
                ((MainActivity)getActivity()).setStatusBarTintResource(R.color.transparent);
            }
        }else {
            if (getActivity() != null){
                ((MainActivity)getActivity()).setStatusBarTintResource(R.color.colorPrimaryDark);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        getInfo();
    }
}
